﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TypeConversions
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with type convetsions *****");
            Console.WriteLine();

            short num1 = 30000, num2 = 30000;
            //强制转化int为short（允许数据丢失）
            short answer = (short)Add(num1, num2);
            Console.WriteLine("{0} + {1} = {2}",
                num1, num2, answer);
            NarrowingAttempt();
            ProcessBytes();

            Console.ReadLine();
        }

        private static void NarrowingAttempt()
        {
            byte myByte = 0;
            int myInt = 200;
            myByte = (byte)myInt;
            Console.WriteLine("Value of myByte: {0}", myByte);
        }

        private static int Add(int x, int y)
        {
            return x + y;
        }

        private static void ProcessBytes()
        {
            byte b1 = 100;
            byte b2 = 250;
            try
            {
                checked
                {
                    byte sum = (byte)Add(b1, b2);
                    Console.WriteLine("sum = {0}", sum);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}
